//
// Created by ins on 2021/3/7.
//

#include "EnemyPresenter.h"
#include "MidEnemyBean.h"
#include "EnemyBean.h"
#include "EnemyDao.h"
#include "json.h"

EnemyDao *enemyDao;
using std::string;
void addMidEnemy(string data, ResData& res_data);
void delMidEnemy(string data, ResData& res_data);
void updateMidEnemy(string data, ResData& res_data);
void getMidEnemy(string data, ResData& res_data);
void getYEnemy(string data, ResData& res_data);

string list2json(list<MidEnemyBean> list);

moduleHandler enemy_cmds[] = {
        {"01", addMidEnemy},
        {"02", delMidEnemy},
        {"03", updateMidEnemy},
        {"04", getMidEnemy},
        {"05", getYEnemy}
};

void addMidEnemy(string data, ResData& res_data){
    MidEnemyBean midEnemyTmp;
    
    Json::Value root = Util::init_root(data);
    if(!root["id"].isNull()){
        midEnemyTmp.setMid(root["nowid"].asString());
        midEnemyTmp.setGname(root["gname"].asString());
        midEnemyTmp.setGlv(root["glv"].asString());
        midEnemyTmp.setGhp(root["ghp"].asString());
        midEnemyTmp.setGgj(root["ggj"].asString());
        midEnemyTmp.setGfy(root["gfy"].asString());
        midEnemyTmp.setGgj(root["gbj"].asString());
        midEnemyTmp.setGxx(root["gxx"].asString());
        midEnemyTmp.setGexp(root["gexp"].asString());
        midEnemyTmp.setGyid(root["gyid"].asString());
        midEnemyTmp.setGmaxhp(root["ghp"].asString());

        if(enemyDao->addMidEnemy(midEnemyTmp)){
            res_data.resp_code = ERRCD_PASS;
            //res_data.resp_data = "\"ok\"";
        }else{
            res_data.resp_code = ERRCD_FALSE;
        }
    }
}
void delMidEnemy(string data, ResData& res_data){}
void updateMidEnemy(string data, ResData& res_data){}
void getMidEnemy(string data, ResData& res_data){

    list<MidEnemyBean> cxMidEnemys;
    Json::Value root = Util::init_root(data);

    if(!root["nowmid"].isNull()){
        string nowMid = root["nowmid"].asString();
        enemyDao->getMidEnemy(nowMid, cxMidEnemys);

        //ay no enemy;
        if(cxMidEnemys.size() != 0){
            res_data.resp_code = ERRCD_PASS;
            res_data.resp_data = list2json(cxMidEnemys);
        }else{
            res_data.resp_code = ERRCD_NONE;
            res_data.resp_data = NONEACKDATA;
        }
    }
}

string list2json(list<MidEnemyBean> list) {
    string result = "\"midEnemy\":[";
    for(MidEnemyBean midEnemyBean: list){
        result += midEnemyBean.toString();
        result += ",";
    }
    int pos = result.rfind(",");
    result.replace(pos,1,"]");
    return result;
}

void getYEnemy(string data, ResData& res_data){
    EnemyBean cxEnemy;

    Json::Value root = Util::init_root(data);
    if(!root["id"].isNull()){
        string gwid = root["id"].asString();
        enemyDao->getYEnemy(gwid, cxEnemy);

        if(cxEnemy.getId() == ""){
            res_data.resp_code = ERRCD_NOMAP;
        }else{
            res_data.resp_code = ERRCD_PASS;
            res_data.resp_data = ""; //TODO
        }
    }
}

EnemyPresenter::EnemyPresenter() {}
EnemyPresenter::~EnemyPresenter() {}

void EnemyPresenter::EnemyMsgHandler(string cmd, string data, ResData &outData) {
    cout << "enemy: " << data << endl;
    int i = 0;

    if(cmd.empty() || data.empty()){
        return;
    }

    for (i = 0; !enemy_cmds[i].cmd.empty(); ++i)
    {
        if (cmd != enemy_cmds[i].cmd)
            continue;
        break;
    }

    enemy_cmds[i].func(data, outData);
}